package NC32_求平方根;

public class Solution {
    /**
     *
     * @param x int整型
     * @return int整型
     */
    public int sqrt (int x) {
        // write code here

        long left = 1, right = x;
        while (right >= left){
            long mid = (left + right) / 2;

            if (mid * mid <= x && (mid + 1) * (mid + 1) > x){
                return (int)mid;
            }
            if (mid * mid > x){
                right = mid - 1;
            }
            if ((mid + 1) * (mid + 1) <= x){
                left = mid + 1;
            }
        }
        return 0;
    }

}